package fr.cavalier.sort;

import org.junit.Assert;
import org.junit.Test;

import fr.cavalier.utils.ArrayUtils;

public class ArrayUtilsTest {

	@Test
	public void isSortedtest(){
		int[] sorted = {1, 4, 6, 13};
		int[] unSorted = {1, 4, 3, 5};
		Assert.assertTrue(ArrayUtils.isSorted(sorted));
		Assert.assertFalse(ArrayUtils.isSorted(unSorted));
	}
	
	@Test
	public void getFirstOccurenceTest() throws Exception{
		int[] array = {1, 4, 6, 13};
		Assert.assertEquals(ArrayUtils.firstOccurenceOf(1, array), 0);
		Assert.assertNotEquals(ArrayUtils.firstOccurenceOf(1, array), 2);
	}
	
	@Test(expected=Exception.class)
	public void getFirstOcurrenceException() throws Exception{
		int[] array = {1, 4, 6, 13};
		ArrayUtils.firstOccurenceOf(5, array);
	}
	
	@Test
	public void dichotomousTest() throws Exception{
		int[] array = {1, 4, 6, 13};
		Assert.assertEquals(ArrayUtils.dichotomousSearch(6, array), 2);
		Assert.assertNotEquals(ArrayUtils.dichotomousSearch(1, array), 2);
		Assert.assertEquals(ArrayUtils.dichotomousSearch(4, array), 1);
	}
	
	@Test
	public void getFirstPositionInArrayTest() throws Exception{
		int[] array = {1, 4, 6, 13};
		Assert.assertEquals(ArrayUtils.getFirstPositionInArray(6, array), 2);
		Assert.assertNotEquals(ArrayUtils.getFirstPositionInArray(1, array), 2);
		Assert.assertEquals(ArrayUtils.getFirstPositionInArray(4, array), 1);	
		
		int[] arrayBis = {1, 8, 6, 4};
		Assert.assertEquals(ArrayUtils.getFirstPositionInArray(6, arrayBis), 2);
		Assert.assertNotEquals(ArrayUtils.getFirstPositionInArray(1, arrayBis), 2);
		Assert.assertEquals(ArrayUtils.getFirstPositionInArray(4, arrayBis), 3);	
	}
	
	@Test(expected=Exception.class)
	public void dichotomousException() throws Exception{
		int[] array = {1, 4, 6, 13};
		ArrayUtils.dichotomousSearch(5, array);
	}
}
